//
//  SearchViewController.m
//  project_delicious
//
//  Created by dlios on 15-3-27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchViewController.h"
#import "CollectionViewController.h"

@interface SearchViewController ()<UISearchBarDelegate,  UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UISearchBar *searchBar;

@end

@implementation SearchViewController
//转菊花
- (void)mbHud
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.labelText = @"Loading...";
    //hud.detailsLabelText = @"正在为您加载";
    [self.view addSubview:hud];
    [hud release];
    [hud show:YES];
}
//tabbar不隐藏
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    // Do any additional setup after loading the view.
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 12)];
    _searchBar.delegate = self;
    _searchBar.placeholder = @"请输入关键字";
    [self.view addSubview:_searchBar];
    [_searchBar release];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _searchBar;
    

    [self.view addSubview:_tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"guo"];
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"guo"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"guo"] autorelease];
    }
//    cell.textLabel.text =
//    NSUInteger row = [indexPath row];
//    if (_searchStr.length != 0) {
//        cell.textLabel.text = [NSString stringWithFormat:@"%@%lu",_searchStr,row];
//    }
    return cell;
}
- (void)passValue:(NSString *)value{
    if (value) {
        _searchBar.text = value;
        [self searchBarSearchButtonClicked:_searchBar];
    }
    else {
        
    }
}
- (void)setSearchControllerHidden:(BOOL)hidden
{
    NSInteger height = hidden ? 180 : 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
//    [_searchList.view setFrame:CGRectMake(30, 36, 200, height)];
    [UIView commitAnimations];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"change");
    if (searchBar.text.length == 0) {
//        _searchList.searchText = searchText;
        [self setSearchControllerHidden:YES];
    } else {
        self.searchStr = self.searchBar.text;
        [self setSearchControllerHidden:NO];
    }
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
    for (id cc in [searchBar subviews]) {
        if ([cc isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)cc;
            [button setTitle:@"cancel" forState:UIControlStateNormal];
            
        }
    }
    NSLog(@"shouldbegin");
    return YES;
}
//- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
//{
//    searchBar.text = @"";
//    NSLog(@"didbegin");
//}
//- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
//{
//    NSLog(@"did end");
//    searchBar.showsCancelButton = NO;
//}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search clicks");
//    [self setSearchControllerHidden:YES];
    self.searchStr = [searchBar text];
    NSLog(@"%@", self.searchStr);
    NSString *str = [NSString stringWithFormat:@"http://121.40.54.251:80/HandheldKitchen/api/vegetable/tblvegetable!getVegetableInfo.do?name=%@&child_catalog_name=&taste=&fitting_crowd=&cooking_method=&effect=&page=1&pageRecord=10&is_traditional=0&user_id=&phonetype=0", self.searchStr];
    NSString *resStrP = [NSString stringWithFormat:@"http://121.40.54.251:80/HandheldKitchen/api/vegetable/tblvegetable!getVegetableInfo.do?name=%@&child_catalog_name=&taste=&fitting_crowd=&cooking_method=&effect=&page=", self.searchStr];
    NSString *resStrS = @"&pageRecord=10&is_traditional=0&user_id=&phonetype=0";
    
    NSString *preStr = @"http://121.41.88.179:80/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?vegetable_id=";
    NSString *suffStr = @"&phonetype=2&user_id=&is_traditional=0";
    
    NSString *preStrMat = @"http://121.41.88.179:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=";
    NSString *suffStrMat = @"&type=1&phonetype=0&is_traditional=0";
    
    NSString *preStrCook = @"http://121.41.88.179:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=";
    NSString *suffStrCook = @"&type=2&phonetype=0&is_traditional=0";
    
    
    CollectionViewController *collect = [[CollectionViewController alloc] init];
    collect.str = str;
    collect.reStrP = resStrP;
    collect.reStrS = resStrS;
    collect.preStr = preStr;
    collect.suffStr = suffStr;
    collect.preStrMat = preStrMat;
    collect.suffStrMat = suffStrMat;
    collect.preStrCook = preStrCook;
    collect.suffStrCook = suffStrCook;

    [self.navigationController pushViewController:collect animated:YES];
    [searchBar resignFirstResponder];
    [_tableView reloadData];
    [collect release];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"cancel");
    searchBar.text = @"";
    [searchBar resignFirstResponder];
    [self setSearchControllerHidden:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
